﻿using System;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.Web.Security;

/// <summary>
/// Summary description for WebService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
// [System.Web.Script.Services.ScriptService]
public class SubService : System.Web.Services.WebService
{

    
    
    public SubService()
    {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public bool ValidateLogin(string userName,string password)
    {
        return Membership.ValidateUser(userName, password);
    }

    [WebMethod]
    public void LogIn(string username, string password)
    {

        bool validate = Membership.ValidateUser(username, password);
        
        if (validate)
        {
            SqlConnection connection = new SqlConnection(DataAccess.GetConnectionString());
            SqlCommand command = new SqlCommand("LoggedIn", connection);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@UserName", SqlDbType.NVarChar, 256).Value = username;
            connection.Open();
            int rows = command.ExecuteNonQuery();
            connection.Close();
            
        }
        
        
            
        
    }

    public SqlConnection GetConnection(string connectionstring)
    {
        SqlConnection connection = new SqlConnection(connectionstring);
        connection.Open();
        return connection;
    }
    

    [WebMethod]
    public int CreateGame(string user)
    {
        SqlConnection connection = new SqlConnection(DataAccess.GetConnectionString());
        SqlCommand command = new SqlCommand("BEGINGAME", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@UserName", SqlDbType.NVarChar, 256).Value = user;
        connection.Open();
        int rows = command.ExecuteNonQuery();
        connection.Close();
        return rows;
    }

    [WebMethod]
    public int CommitGame(string user, string oponnent)
    {
        SqlConnection connection = new SqlConnection(DataAccess.GetConnectionString());
        SqlCommand command = new SqlCommand("CommitGame", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@UserName", SqlDbType.NVarChar, 256).Value = user;
        command.Parameters.Add("@OponnentU", SqlDbType.NVarChar, 256).Value = oponnent;
        connection.Open();
        int rows = command.ExecuteNonQuery();
        connection.Close();
        return rows;
    }

    [WebMethod]
    public string[] GetOpennedGames()
    {
        ArrayList gameList = new ArrayList();
        SqlConnection connection = new SqlConnection(DataAccess.GetConnectionString());
        SqlCommand command = new SqlCommand("GetOpennedGames", connection);
        command.CommandType = CommandType.StoredProcedure;
        connection.Open();
        SqlDataReader reader = command.ExecuteReader();
        while (reader.Read())
        {
            gameList.Add(reader.GetString(0));
        }
       connection.Close();
       string[] games = new string[gameList.Count];
       gameList.CopyTo(games);
        return games;
    }

    [WebMethod]
    public int LogOut(string user)
    {
        SqlConnection connection = new SqlConnection(DataAccess.GetConnectionString());
        SqlCommand command = new SqlCommand("LogOut", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@UserName", SqlDbType.NVarChar, 256).Value = user;
        connection.Open();
        int rows = command.ExecuteNonQuery();
        connection.Close();
        return rows;
    }

    [WebMethod]
    public int GameOver(string user, bool loss)
    {
        SqlConnection connection = new SqlConnection(DataAccess.GetConnectionString());
        SqlCommand command = new SqlCommand("EndGame", connection);
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.Add("@UserName", SqlDbType.NVarChar, 256).Value = user;
        command.Parameters.Add("@Loss", SqlDbType.Bit).Value = loss;
        connection.Open();
        int rows = command.ExecuteNonQuery();
        connection.Close();
        return rows;
    }










   
     
    

    
}

